package com.hibase.core.page.controller;

import com.github.pagehelper.PageInfo;
import com.hibase.common.entity.ResponseModel;
import com.hibase.common.exception.HibaseAssert;
import com.hibase.common.util.EntityUtil;
import com.hibase.core.page.constant.PageUserConfigType;
import com.hibase.core.page.entity.pageuserconfig.PageUserConfig;
import com.hibase.core.page.entity.pageuserconfig.vo.*;
import com.hibase.core.page.service.pageuserconfig.PageUserConfigService;
import com.hibase.core.web.annotation.HibasePostRequestMapping;
import com.hibase.core.web.entity.OperationIdEntity;
import com.hibase.core.web.entity.OperationUserIdEntity;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * <p>
 * 用户菜单配置表 前端控制器
 * </p>
 *
 * @author junk
 * @since 2021-03-31
 */
@RestController
@RequestMapping("/page-user-config")
@Api(tags = "用户菜单配置表")
@SuppressWarnings({"unchecked", "rawtypes"})
public class PageUserConfigController{

    @Autowired
    private PageUserConfigService pageUserConfigService;

    @ApiOperation(value = "查询用户菜单配置表列表", httpMethod = "POST")
    @HibasePostRequestMapping(value = "selectPageUserConfigList")
    public ResponseModel<PageUserConfigListVO> selectPageMenuList(@RequestBody PageUserConfigSearchVO searchVO) {

        PageInfo result = pageUserConfigService.selectPageUserConfigList(searchVO);

        result.setList(EntityUtil.transform(result.getList(), PageUserConfigListVO.class));

        return ResponseModel.successMsg(result);
    }

    @ApiOperation(value = "新增用户菜单配置表记录", httpMethod = "POST")
    @HibasePostRequestMapping(value = "insertPageUserConfig")
    public ResponseModel<String> insertPageUserConfig(@RequestBody PageUserConfigInsertVO insertVO) {

        PageUserConfig entity = EntityUtil.transform(insertVO, PageUserConfig.class);

        pageUserConfigService.savePageUserConfig(entity);

        return ResponseModel.successMsg(entity.getId());
    }

    @ApiOperation(value = "修改用户菜单配置表记录", httpMethod = "POST")
    @HibasePostRequestMapping(value = "updatePageUserConfig")
    public ResponseModel<String> updatePageUserConfig(@RequestBody PageUserConfigUpdateVO updateVO) {

        PageUserConfig entity = EntityUtil.transform(updateVO, PageUserConfig.class);

        pageUserConfigService.savePageUserConfig(entity);

        return ResponseModel.successMsg(entity.getId());
    }


    @ApiOperation(value = "修改用户菜单配置表记录", httpMethod = "POST")
    @HibasePostRequestMapping(value = "savePageUserConfig")
    public ResponseModel<String> savePageUserConfig(@RequestBody PageUserConfigUpdateVO updateVO) {

        PageUserConfig entity = EntityUtil.transform(updateVO, PageUserConfig.class);

        pageUserConfigService.savePageUserConfig(entity);

        return ResponseModel.successMsg(entity.getId());
    }

    @ApiOperation(value = "查询用户菜单配置表详情", httpMethod = "POST")
    @HibasePostRequestMapping(value = "viewPageUserConfigDetails")
    public ResponseModel<PageUserConfigViewVO> viewPageUserConfigDetails(@RequestBody OperationIdEntity operationidEntity) {

        return ResponseModel.successMsg(EntityUtil.transform(pageUserConfigService.viewPageUserConfig(operationidEntity.getId()), PageUserConfigViewVO.class));
    }

    @ApiOperation(value = "删除用户菜单配置表记录", httpMethod = "POST")
    @HibasePostRequestMapping(value = "/deletePageUserConfig")
    public ResponseModel<String> deletePageUserConfig(@RequestBody OperationIdEntity delete) {

        pageUserConfigService.deletePageUserConfig(delete.getId());

        return ResponseModel.successMsg();
    }

    @ApiOperation(value = "导出用户菜单配置表记录", httpMethod = "POST")
    @HibasePostRequestMapping(value = "/exportPageUserConfig")
    public ResponseModel<String> exportPageUserConfig(@RequestBody PageUserConfigSearchVO searchVO, HttpServletResponse response) {

        pageUserConfigService.exportPageUserConfig(searchVO, response);

        return ResponseModel.successMsg();
    }
    
    /**
     * @Title: PageUserConfigController
     * @Description: 查询页面初始化条件;BY 用户, BY pageId, BY type
     * @author TimBrian
     * @since 2022年2月16日 上午11:31:35
     * @param searchVo
     * @return
     */
	@ApiOperation(value = "查询页面初始化条件，BY 用户 BY pageId", httpMethod = "POST")
	@HibasePostRequestMapping(value = "getSelectByUserIdAndPageId")
	public ResponseModel<PageUserConfigViewVO> getSelectByUserIdAndPageId(@RequestBody OperationUserIdEntity searchVo) {
		PageUserConfig selectByUserIdAndPageId = pageUserConfigService.getSelectByUserIdAndPageId(searchVo);
		return ResponseModel.successMsg(EntityUtil.transform(selectByUserIdAndPageId, PageUserConfigViewVO.class));
	}
	
	/**
     * @Title: PageUserConfigController
     * @Description: 保存查询页面初始化条件;BY 用户, BY pageId, BY type
     * @author TimBrian
     * @since 2022年2月16日 上午11:31:35
     * @param updateVO
     * @return
     */
	@ApiOperation(value = "修改用户菜单配置表记录", httpMethod = "POST")
    @HibasePostRequestMapping(value = "saveSelectByUserIdAndPageId")
    public ResponseModel<String> saveSelectByUserIdAndPageId(@RequestBody PageUserConfigUpdateNewVO updateVO) {
		HibaseAssert.isTrue(PageUserConfigType.SELECT.getKey().equals(updateVO.getType()), "类型不匹配");
        PageUserConfig entity = EntityUtil.transform(updateVO, PageUserConfig.class);
        entity.setConfigJson(JSONUtil.toJsonStr(updateVO.getConfigJson()));
        pageUserConfigService.savePageUserConfig(entity);
        return ResponseModel.successMsg(entity.getId());
    }
	
}
